#!/usr/bin/env python
# --coding:utf-8-- 
"""
@File    : GlobalVar.py
@Time    : 2021/7/27 14:56
@Author  : cpl
"""
import queue
import csv
import pandas as pd
import requests

def _init():
    global url
    url = "neo4j://localhost:7687"

    global pro_queue
    pro_queue = queue.Queue()
    # pro_queue.put('no-one-left-behind-v2018.2.10')

    global seen
    seen = set()
    # seen.add('no-one-left-behind-v2018.2.10')

    global seen_all
    seen_all = set()

    global miss_pkgs
    miss_pkgs = {}

    global  no_version_pkgs
    no_version_pkgs = {}

    global order_num
    order_num = 0

    global pkg_list
    pkg_list = []


    global allv_pkg_num
    allv_pkg_num = 0


def set_pkg_list(file='pkg.csv'):
    pkg = pd.read_csv(file)
    global pkg_list
    pkg_list = (list(pkg['name']))
    print("get %d projects from pkg.csv"%(len(pkg_list)))

def test():
    global pkg_list
    pkg_list = ['mongodb']
    print("get %d projects: %s from test" % (len(pkg_list), pkg_list[0]))

def crawl_pkg_list(st, ed): # 单位100
    global pkg_list
    js_star_list = []
    for i in range(st, ed+1):
        url = 'https://api.github.com/search/repositories?q=language:JavaScript&sort=stars&page=%s&per_page=100' % i
        r = requests.get(url)
        response_dict = r.json()
        repo_dicts = response_dict['items']
        for repo_dict in repo_dicts:
            pkg_list.append(repo_dict['name'])
            js_star_list.append(repo_dict['stargazers_count'])
    print("get %d most popular projects from github, not all in npm"%(len(js_star_list)))
    record = open("summary.txt", "a")
    record.write('-------------------------------------------------------\n')
    record.write("GET %d popular projects from github page %d to %d, not all in npm"%(len(js_star_list), st, ed))
    record.close()

def get_pkg_list():
    return pkg_list

def add_queue(name, version):
    pro_queue.put(name)
    pro_queue.put(version)


def pop_queue():
    name = pro_queue.get()
    version = pro_queue.get()
    return name, version


def get_qsize():
    # 一个项目包括名字和版本
    return pro_queue.qsize()//2


def add_seen(pkg_id):
    seen.add(pkg_id)


def not_seen(pkg_id):
    return pkg_id not in seen

def add_seenall(pkg_id):
    seen_all.add(pkg_id)


def not_seeall(pkg_id):
    return pkg_id not in seen_all

def add_miss(name, status_code):
    miss_pkgs[name] = status_code


def count_miss():
    return len(miss_pkgs)


def clear_miss():
    miss_pkgs.clear()


def write_miss(name, vnum):
    f = open('misspkgs.csv', 'a')
    writer = csv.writer(f)
    writer.writerow([name, str(vnum)+' versions'])
    f.close()

    f = open('misspkgs.csv', 'a')
    writer = csv.writer(f)
    writer.writerow(['miss_pkg_name', 'status_code'])
    for k, v in miss_pkgs.items():
        writer.writerow([k, v])

    writer.writerow(['    ', '    '])
    f.close()

def add_noversionPkg(name, info):
    no_version_pkgs[name] = info


def count_noversion():
    return len(no_version_pkgs)


def clear_noversion():
    no_version_pkgs.clear()


def write_noversion(name, vnum):
    f = open('noversion.csv', 'a')
    writer = csv.writer(f)
    writer.writerow([name, str(vnum) + ' versions'])
    f.close()

    f = open('noversion.csv', 'a')
    writer = csv.writer(f)
    writer.writerow(['pkg_name', 'info'])
    for k, v in no_version_pkgs.items():
        writer.writerow([k, v])

    writer.writerow(['    ', '    '])
    f.close()


def add_ordernum(order=1):
    global order_num
    order_num += order


def get_ordernum():
    return order_num


def clear_ordernum():
    global order_num
    order_num = 0


def add_allvnum(num=1):
    global allv_pkg_num
    allv_pkg_num += num


def get_allvnum():
    return allv_pkg_num


def clear_allvnum():
    global allv_pkg_num
    allv_pkg_num = 0